package views;

import DAO.InterTabDao;
import Entity.InterTab;
import util.NotNull;
import util.Result;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class NetworkManagement {


    static  final  String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static  final  String DB_URL = "jdbc:mysql://192.168.126.129:20000/tour";
    static  final  String USER= "swpu_user";
    static  final  String PASS = "2121709a*";
    /*添加到数据库所必需的代码*/
    static Connection connect = null;
    static PreparedStatement cmmd = null;
    static ResultSet rs = null;
    static String sql = "";
    private static Object Result;


    JLabel l4;
    JLabel l1;
    JLabel l2;
    JLabel l3;
    JTextField InterID;   //网点编号
    JTextField InterName;  //网点名称
    JTextField InterTel;   //网点电话
    JTextField InterLocat;   //网店地址
    JButton btnOK;           //保存
    JButton btn3;            //重置
    JButton breturn;
    int flag;
    InterTab interTab = new InterTab();
    InterTabDao interTabDao = new InterTabDao();
    TextArea area;

    class PanelBg extends  JPanel{
        Dimension d;
        Image image;
        PanelBg(Dimension d,Image image){
            this.d=d;
            this.image=image;
        }
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(image,0,0,d.width,d.height,this);
        }
    }
    Dimension fmsize=new Dimension(600,700);
    ImageIcon imageicon = new ImageIcon(this.getClass().getResource("/erhai4.jpg"));

    <interTab> NetworkManagement(Result result){

        JFrame jframe = new JFrame();
        jframe.setSize(600,700);
        jframe.setDefaultCloseOperation(jframe.DISPOSE_ON_CLOSE);
        jframe.setLayout(null);
        jframe.setLocationRelativeTo(null);


        PanelBg pContent = new PanelBg(fmsize,imageicon.getImage());
        pContent.setLayout(null);
        pContent.setBounds(0,0,600,700);
        jframe.getContentPane().add(pContent);



        l1 = new JLabel("网点编号");
        l1.setBounds(100,80,70,30);
        pContent.add(l1);

        InterID = new JTextField();
        InterID.setBounds(180,80,250,40);
        InterID.setEnabled(false);
        pContent.add(InterID);


        l2 = new JLabel("网点名称");
        l2.setBounds(100,180,70,30);
        pContent.add(l2);

        InterName = new JTextField();
        InterName.setBounds(180,180,250,40);
        String vInterName = InterName.getText();
        pContent.add(InterName);

        l3 = new JLabel("网点电话");
        l3.setBounds(100,310,70,30);
        pContent.add(l3);

        InterTel = new JTextField();
        InterTel.setBounds(180,310,250,40);
        String vInterTel = InterTel.getText();
        pContent.add(InterTel);


        l4 = new JLabel("网点地址");
        l4.setBounds(100,440,70,30);
        pContent.add(l4);

        InterLocat = new JTextField();
        InterLocat.setBounds(180,440,250,40);
        String vInterLocat = InterLocat.getText();
        pContent.add(InterLocat);


        if ((result.getStatus().equals("1"))) {
            try {
                System.out.println(interTabDao.justgetID());
                InterID.setText(Integer.valueOf(interTabDao.justgetID())+1+"");
            } catch ( SQLException throwables) {
                throwables.printStackTrace();
            }
            flag=1;
        }
        if(result.getStatus().equals("0")){
            InterID.setText((String) result.getMessage());
            String vInterID = InterID.getText();
            interTab = interTabDao.get_byID(vInterID);
            InterID.setText(interTab.getInterID());
            InterName.setText(interTab.getInterName());
            InterTel.setText(interTab.getInterTel());
            InterLocat.setText(interTab.getInterLocat());
            flag=0;
        }


        btn3 =new JButton("保存");
        btn3.setBounds(180,540,70,50);
        pContent.add(btn3);
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if((new NotNull().IsNull(InterID,InterName,InterTel))==false){
                    JOptionPane.showMessageDialog(null,"不能为空");
                    return;
                }
                if(flag==1){
//                    InterID.setText((String) result.getMessage());
                    String vInterID = InterID.getText();
                    String vInterName = InterName.getText();
                    String vInterTel = InterTel.getText();
                    String vInterLocat = InterLocat.getText();

                    interTab.setInterID(vInterID);
                    interTab.setInterName(vInterName);
                    interTab.setInterTel(vInterTel);
                    interTab.setInterLocat(vInterLocat);
                    interTabDao.insert(interTab);
                    JOptionPane.showMessageDialog(null,"新增成功");
                }
                if(flag==0) {
//                    InterID.setText((String) result.getMessage());
                    String vInterID = InterID.getText();
                    String vInterName = InterName.getText();
                    String vInterTel = InterTel.getText();
                    String vInterLocat = InterLocat.getText();
//                    InterTab interTab = new InterTab();
//                    InterTabDao interTabDao = new InterTabDao();
                    interTab.setInterID(vInterID);
                    interTab.setInterName(vInterName);
                    interTab.setInterTel(vInterTel);
                    interTab.setInterLocat(vInterLocat);
                    interTabDao.insert(interTab);
                    interTabDao.update3(interTab);
                    JOptionPane.showMessageDialog(null,"修改成功");
                }

            }
        });


        breturn = new JButton("退出");
        breturn.setBounds(270,540,70,50);
        breturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jframe.setVisible(false);
            }
        });
        pContent.add(breturn);


        btnOK = new JButton("重置");
        btnOK.setBounds(360,540,70,50);
        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InterID.setText("");
                InterName.setText("");
                InterTel.setText("");
                InterLocat.setText("");
            }
        });
        pContent.add(btnOK);

        jframe.setVisible(true);

    }

//    public static void main(String[] args) {
//        Result result = new Result();
//        result.setMessage("1");
//        result.setStatus("0");
//        new NetworkManagement(result);
//    }

}
